//
// Created by xili on 2024/10/10 18:21.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int minimumSum(vector<int> &nums) {
        int n = nums.size();
        int ans = INT32_MAX;

        vector<int> suf(n);//维护后缀最小值
        int cur = nums[n - 1];
        for (int i = n - 1; i >= 0; i--) {
            cur = min(cur, nums[i]);
            suf[i] = cur;
        }
        int pre = nums[0];
        for (int i = 1; i < n - 1; i++) {
            if (nums[i] > pre && nums[i] > suf[i]) {
                ans = min(ans, pre + nums[i] + suf[i]);
            }
            pre = min(pre, nums[i]);
        }
        return ans == INT32_MAX ? -1 : ans;
    }
};